home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / docie.ps < prev    next >
Text File  |  1996-01-10  |  7KB  |  220 lines

  1. %    Copyright (C) 1995 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % docie.ps
  16. % Emulate CIE algorithms in PostScript.
  17.  
  18. % ---------------- Auxiliary procedures ---------------- %
  19.  
  20. /r1default [0 1] def
  21. /r3default [0 1 0 1 0 1] def
  22.  
  23. /apply3        % <u> <v> <w> [<pu> <pv> <pw>] apply3 <u'> <v'> <w'>
  24.  {  { 4 -1 roll exch exec } forall
  25.  } bind def
  26.  
  27. /restrict    % <u> <min> <max> restrict <u'>
  28.  { 3 1 roll .max .min
  29.  } bind def
  30.  
  31. /restrict3    % <u> <v> <w> [<minu> ... <maxw>] restrict3 <u'> <v'> <w'>
  32.  { aload pop
  33.    7 -1 roll 3 1 roll restrict 7 1 roll
  34.    5 -1 roll 3 1 roll restrict 5 1 roll
  35.    restrict 3 1 roll
  36.  } bind def
  37.  
  38. /rescale    % <u> <min> <max> rescale <u'>
  39.  { 1 index sub 3 1 roll sub exch div 0 .max 1 .min
  40.  } bind def
  41.  
  42. /rescale3    % <u> <v> <w> [<minu> ... <maxw>] rescale3 <u'> <v'> <w'>
  43.  { aload pop
  44.    7 -1 roll 3 1 roll rescale 7 1 roll
  45.    5 -1 roll 3 1 roll rescale 5 1 roll
  46.    rescale 3 1 roll
  47.  } bind def
  48.  
  49. /mmult3        % <u> <v> <w> [<uu> <uv> ... <wv> <ww>] mmult3
  50.         %   <u'> <v'> <w'>
  51.  { 4 -1 roll dup dup 6 -1 roll dup dup 8 -1 roll dup dup
  52.    10 -1 roll { 10 -1 roll mul } forall
  53.         % Stack: u1 v1 w1 u2 v2 w2 u3 v3 w3
  54.    4 -1 roll add 6 -1 roll add
  55.         % Stack: u1 v1 u2 v2 u3 v3 w'
  56.    7 1 roll 3 -1 roll add 4 -1 roll add
  57.         % Stack: w' u1 u2 u3 v'
  58.    5 1 roll add add 3 1 roll
  59.  } bind def
  60.  
  61. /minvert3    % [<uu> <uv> ... <wv> <ww>] minvert3
  62.         %   [<uu'> <uv'> ... <wv'> <ww'>]
  63.  { 16 dict begin
  64.    aload pop { I H G F E D C B A } { exch def } forall
  65.    /coa E I mul F H mul sub def
  66.    /cob F G mul D I mul sub def
  67.    /coc D H mul E G mul sub def
  68.    /det A coa mul B cob mul add C coc mul add def
  69.    [ coa det div
  70.      C H mul B I mul sub det div
  71.      B F mul C E mul sub det div
  72.      cob det div
  73.      A I mul C G mul sub det div
  74.      C D mul A F mul sub det div
  75.      coc det div
  76.      B G mul A H mul sub det div
  77.      A E mul B D mul sub det div
  78.    ]
  79.    end
  80.  } bind def
  81.  
  82. /print1
  83.  { print dup ==
  84.  } bind def
  85.  
  86. /print3
  87.  { print 3 array astore dup == aload pop
  88.  } bind def
  89.  
  90. % ---------------- Mapping to XYZ ---------------- %
  91.  
  92. /csmap        % <csdict> <l> <m> <n> csmap <csdict> <x> <y> <z>
  93.  { 3 index /RangeLMN .knownget not { r3default } if restrict3
  94. DEBUG { (After RangeLMN Decode: ) print3 } if
  95.    3 index /DecodeLMN .knownget { apply3 } if
  96. DEBUG { (After DecodeLMN Decode: ) print3 } if
  97.    3 index /MatrixLMN .knownget { mmult3 } if
  98. DEBUG { (After MatrixLMN Decode: ) print3 } if
  99.  } bind def
  100.  
  101. /csciea        % <csdict> <a> csciea <csdict> <x> <y> <z>
  102.  { 1 index /RangeA .knownget not { r1default } if restrict
  103. DEBUG { (After RangeA Decode: ) print1 } if
  104.    1 index /DecodeA .knownget { exec } if
  105. DEBUG { (After DecodeA Decode: ) print1 } if
  106.    1 index /MatrixA .knownget
  107.     { { 1 index mul exch } forall pop }
  108.     { dup dup }
  109.    ifelse
  110. DEBUG { (After MatrixA Decode: ) print3 } if
  111.    csmap
  112.  } bind def
  113.  
  114. /cscieabc    % <csdict> <a> <b> <c> cscieabc <csdict> <x> <y> <z>
  115.  { 3 index /RangeABC .knownget not { r3default } if restrict3
  116. DEBUG { (After RangeABC Decode: ) print3 } if
  117.    3 index /DecodeABC .knownget { apply3 } if
  118. DEBUG { (After DecodeABC Decode: ) print3 } if
  119.    3 index /MatrixABC .knownget { mmult3 } if
  120. DEBUG { (After MatrixABC Decode: ) print3 } if
  121.    csmap
  122.  } bind def
  123.  
  124. % ---------------- Rendering from XYZ ---------------- %
  125.  
  126. /lookup3    % <rtable> <a[0..1]> <b[0..1]> <c[0..1]> lookup3
  127.         %   <rtable> <bytes>
  128.  { 3 -1 roll 3 index 0 get 1 sub mul
  129.    3 -1 roll 3 index 1 get 1 sub mul
  130.    3 -1 roll 3 index 2 get 1 sub mul
  131.         % Stack: rtable ia ib ic
  132. DEBUG { (RenderTable indices: ) print3 mark 5 1 roll } if
  133.    3 -1 roll round cvi 3 index 3 get exch get
  134.         % Stack: rtable ib ic string
  135.    3 -1 roll round cvi 3 index 2 get mul
  136.         % Stack: rtable ic string ib*nc
  137.    3 -1 roll round cvi add 2 index 4 get mul
  138.         % Stack: rtable string index
  139.    2 index 4 get getinterval
  140.         % Stack: rtable bytes
  141. DEBUG { (RenderTable values: ) print (<) print (%stdout) (w) file 1 index writehexstring (>) = } if
  142.  } bind def
  143.  
  144. /bpdefault [0 0 0] def
  145.  
  146. /crmap        % <csdict> <crdict> <x> <y> <z> crmap <v1> ...
  147.  {
  148. DEBUG { (CIE XYZ = ) print3 } if
  149.    3 index /MatrixPQR .knownget { mmult3 } if
  150. DEBUG { (After MatrixPQR: ) print3 } if
  151.    4 index /WhitePoint get
  152.    5 index /BlackPoint .knownget not { bpdefault } if
  153.    5 index /WhitePoint get
  154.    6 index /BlackPoint .knownget not { bpdefault } if
  155.    4
  156.      { 4 -1 roll aload pop
  157.             % Stack: csdict crdict x y z pt pt pt px py pz
  158.        3 copy 12 index /MatrixPQR .knownget { mmult3 } if 6 array astore
  159.      }
  160.    repeat
  161.             % Stack: csdict crdict x y z wps+ bps+ wpd+ bpd+
  162.    9 -1 roll pop        % get rid of csdict
  163.    7 4 roll
  164.    7 index /TransformPQR get
  165.     {            % Stack: crdict wps+ bps+ wpd+ bpd+ u v w proc
  166.       8 copy exch pop exch pop
  167.       exec exch pop 4 -1 roll pop
  168.     }
  169.    forall
  170.    7 3 roll pop pop pop pop    % get rid of White/BlackPoints
  171. DEBUG { (After TransformPQR: ) print3 } if
  172.    3 index /MatrixPQR .knownget { minvert3 mmult3 } if
  173. DEBUG { (After MatrixPQR': ) print3 } if
  174.    3 index /MatrixLMN .knownget { mmult3 } if
  175. DEBUG { (After MatrixLMN Encode: ) print3 } if
  176.    3 index /EncodeLMN .knownget { apply3 } if
  177. DEBUG { (After EncodeLMN Encode: ) print3 } if
  178.    3 index /RangeLMN .knownget not { r3default } if restrict3
  179. DEBUG { (After RangeLMN Encode: ) print3 } if
  180.    3 index /MatrixABC .knownget { mmult3 } if
  181. DEBUG { (After MatrixABC Encode: ) print3 } if
  182.    3 index /EncodeABC .knownget { apply3 } if
  183. DEBUG { (After EncodeABC Encode: ) print3 } if
  184.    3 index /RangeABC .knownget not { r3default } if
  185.    5 -1 roll /RenderTable .knownget
  186.     {        % Stack: u v w ranges rtable
  187.       5 1 roll rescale3
  188. DEBUG { (Rescaled ABC: ) print3 } if
  189.         % Stack: rtable a b c
  190.       lookup3
  191.         % Stack: rtable bytes
  192.       0 1 3 index 4 get 1 sub
  193.        {    % Stack: values rtable bytes c
  194.      2 copy get 255 div
  195.         % Stack: values rtable bytes c v
  196.      3 index 3 -1 roll 5 add get exec 3 1 roll
  197.        }
  198.       for pop pop
  199. DEBUG { (After RenderTableT: ) print ] dup == aload pop } if
  200.     }
  201.     { restrict3
  202. DEBUG { (After RangeABC Encode: ) print3 } if
  203.     }
  204.    ifelse
  205.  } bind def
  206.  
  207. % ---------------- Top level control ---------------- %
  208.  
  209. /mapdict mark
  210.   /CIEBasedA { 1 get exch csciea currentcolorrendering 4 1 roll crmap } bind
  211.   /CIEBasedABC { 1 get 4 1 roll cscieabc currentcolorrendering 4 1 roll crmap } bind
  212. .dicttomark def
  213. /mapcie        % <a> mapcie <v1> ...
  214.         % <a> <b> <c> mapcie <v1> ...
  215.  { currentcolorspace dup 0 get //mapdict exch get exec
  216.  } bind def
  217.